<?php

/**
 * 删除多余的分类
*/

namespace Admin\Controller;

class DelsuperController extends \Think\Controller{

    public function delCate(){
        $cates = M('goods_category')->where([
            'is_show' => 0,
            'level' => 3
        ])->select();

        $res['level3'] = [
            'failed' => [
                'goods'=> []
            ],
            'success' => 0
        ];
        foreach($cates as $k => $v)
        {
            $goods = M('Goods')->where([
                'cat_id' => $v['id']
            ])->getField('goods_id');
            if( $goods )
            {
                $res['level3']['failed']['goods'][$v['id']] = $goods;
            }else{
                $del = M('goods_category')->where([
                    'id' => $v['id']
                ])->delete();
                if( $del )
                $res['level3']['success']++;
            }
        }

        $cates = M('goods_category')->where([
            'is_show' => 0,
            'level' => 2
        ])->select();

        $res['level2'] = [
            'failed' => [
                'goods'=> [],
                'scates'=> [],
            ],
            'success' => 0
        ];
        foreach($cates as $k => $v)
        {
            $pcates = M('goods_category')->where([
                'parent_id' => $v['id']
            ])->getField('id');
            if( $pcates )
            {
                $res['level2']['failed']['scates'][$v['id']] = $pcates;
                continue;
            }
            $goods = M('Goods')->where([
                'cat_id' => $v['id']
            ])->getField('goods_id');
            if( $goods )
            {
                $res['level2']['failed']['goods'][$v['id']] = $goods;
            }else{
                $del = M('goods_category')->where([
                    'id' => $v['id']
                ])->delete();
                if( $del )
                $res['level2']['success']++;
            }
        }

        $res['level1'] = [
            'failed' => [
                'goods'=> [],
                'scates'=> [],
            ],
            'success' => 0
        ];
        foreach($cates as $k => $v)
        {
            $pcates = M('goods_category')->where([
                'parent_id' => $v['id']
            ])->getField('id');
            if( $pcates )
            {
                $res['level1']['failed']['scates'][$v['id']] = $pcates;
                continue;
            }
            $goods = M('Goods')->where([
                'cat_id' => $v['id']
            ])->getField('goods_id');
            if( $goods )
            {
                $res['level1']['failed']['goods'][$v['id']] = $goods;
            }else{
                $del = M('goods_category')->where([
                    'id' => $v['id']
                ])->delete();
                if( $del )
                $res['level1']['success']++;
            }
        }

        header('content-type:application/json');
        doLog('[分类删除]'.json_encode($res),'delcate');
        echo json_encode($res);
    }

    public function delAttr(){
        $cates = M('goods_category')->where([
            'is_show' => 0,
            'level' => 3
        ])->select();

        $res['level3'] = [
            'failed' => [
                'goods'=> []
            ],
            'success' => 0
        ];
        foreach($cates as $k => $v)
        {
            $goods = M('Goods')->where([
                'cat_id' => $v['id']
            ])->getField('goods_id');
            if( $goods )
            {
                $res['level3']['failed']['goods'][$v['id']] = $goods;
            }else{
                $res['level3']['success']++;
            }
        }

        $cates = M('goods_category')->where([
            'is_show' => 0,
            'level' => 2
        ])->select();

        $res['level2'] = [
            'failed' => [
                'goods'=> [],
                'scates'=> [],
            ],
            'success' => 0
        ];
        foreach($cates as $k => $v)
        {
            $pcates = M('goods_category')->where([
                'parent_id' => $v['id']
            ])->getField('id');
            if( $pcates )
            {
                $res['level2']['failed']['scates'][$v['id']] = $pcates;
                continue;
            }
            $goods = M('Goods')->where([
                'cat_id' => $v['id']
            ])->getField('goods_id');
            if( $goods )
            {
                $res['level2']['failed']['goods'][$v['id']] = $goods;
            }else{
                $res['level2']['success']++;
            }
        }

        $res['level1'] = [
            'failed' => [
                'goods'=> [],
                'scates'=> [],
            ],
            'success' => 0
        ];
        foreach($cates as $k => $v)
        {
            $pcates = M('goods_category')->where([
                'parent_id' => $v['id']
            ])->getField('id');
            if( $pcates )
            {
                $res['level1']['failed']['scates'][$v['id']] = $pcates;
                continue;
            }
            $goods = M('Goods')->where([
                'cat_id' => $v['id']
            ])->getField('goods_id');
            if( $goods )
            {
                $res['level1']['failed']['goods'][$v['id']] = $goods;
            }else{
                $res['level1']['success']++;
            }
        }

        header('content-type:application/json');
        doLog('[分类删除]'.json_encode($res),'delcate');
        echo json_encode($res);
    }
}